package com.home766.template.directive;

import com.home766.dto.DictionaryDTO;
import com.home766.dto.SchemeProductDto;
import com.home766.entity.Scheme;
import com.home766.service.IDictionaryService;
import com.home766.service.SchemeProductService;
import com.home766.service.SchemeService;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import net.biz.Page;
import net.biz.template.directive.BaseDirective;
import net.biz.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页导航-智能方案所有类型查询
 */
@Component("schemeTypeRootListDirective")
public class SchemeTypeRootListDirective extends BaseDirective {

	@Autowired
	private IDictionaryService iDictionaryService;
	
	@Resource(name="schemeServiceImpl")
	private SchemeService schemeService;
	
	@Resource(name="schemeProductServiceImpl")
	private SchemeProductService schemeProductService;
	
	/** 变量名称 */
	private static final String VARIABLE_NAME = "schemeTypes";
	
	@Override
	@SuppressWarnings("rawtypes")
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		List<DictionaryDTO> types = iDictionaryService.searchChildren(ParamsUtil.SCHEME_TYPE);
		
		List<Scheme> schemes = new ArrayList<Scheme>();
		List<Scheme> scheme_s;
		for(DictionaryDTO type : types){
			Page<Scheme> page = schemeService.findPage(null,null,null,null,null,null,type.getId());
//			System.out.println(type.getTitle()+"--"+type.getId()+"---");
			scheme_s = new ArrayList<Scheme>();
			scheme_s.addAll(page.getContent());
			if(!scheme_s.isEmpty()){
				for(Scheme scheme : scheme_s){
					if(scheme.getIsMarketable()){
//						System.out.println("     "+scheme.getName()+"--"+scheme.getType());
						schemes.add(scheme);
					}
				}
			}
		}
		List<SchemeProductDto> brands_warms = schemeProductService.getSchemeProductDtoList();
		List<SchemeProductDto> brands_acs = schemeProductService.getSchemeProductDtoListByConditioner();
		
		/*for(SchemeProductDto brands_warm : brands_warms){
			System.out.println(brands_warm.getName());
			System.out.println(brands_warm.getId());
			System.out.println(brands_warm.getBrand());
		}*/
		
		
		/*for(Scheme scheme : schemes){
			System.out.println(scheme.getName());
		}*/
		//System.out.println("asd");
		
		Map<String, Object> result=new HashMap<String, Object>();
		
		result.put("brands_warms", brands_warms);
		result.put("brands_acs", brands_acs);
		result.put("schemes", schemes);
		result.put("schemeTypesRootList", types);
		setLocalVariable(VARIABLE_NAME, result, env, body);
	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
